﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;

namespace HurricanePackage
{
    public abstract class BaseState<SID,TID>:IStateForFsm<SID,TID>,ICacheable
        where SID : Enum//状态机id
        where TID : Enum//条件id
    {

        //映射状态字典
        [NotNull] private Dictionary<TID, SID> m_relateStateDic;
        //需要判断的条件列表
       [NotNull] private List<ITriggerForFsm<TID>> m_triggerList;

       public static Action<SID> ChangeStateAct;

        public abstract SID StateId { get; }
        public abstract List<TID> TriggerId { get; }


        public abstract void EnterState(params object[] _params);
        public abstract void StayState(params object[] _params);
        public abstract void ExitState(params object[] _params);

        public BaseState()
        {
            m_relateStateDic = new Dictionary<TID, SID>();
            m_triggerList = new List<ITriggerForFsm<TID>>();
        }


        public virtual void Init()
        {
            
        }

        public virtual void Reset()
        {
            m_triggerList?.Clear();
            m_relateStateDic?.Clear();
        }

        public virtual void Discard()
        {
            m_triggerList?.Clear();
            m_triggerList = null;
            m_relateStateDic?.Clear();
            m_triggerList = null;
        }

        public abstract void Recycle();
        /// <summary>
        /// 遍历条件
        /// </summary>
        public void Reason(params object[] _params)
        {
            foreach (var trigger in m_triggerList)
            {
                if(trigger == null)
                    continue;
                //如果条件满足，找到对应的状态id并切换
                if (trigger.HandleTrigger(_params))
                {
                    m_relateStateDic.TryGetValue(trigger.TriggerId, out var stateID);
                    ChangeStateAct?.Invoke(stateID);
                    return;
                }

            }

        }
        /// <summary>
        /// 添加条件关联
        /// </summary>
        public void AddRelation(TID _triggerId, SID _stateId)
        {
            // 缓存池添加
            var trigger = _GetTrigger(_triggerId);
            if (trigger != null)
            {
                m_triggerList.Add(trigger);
                //添加关联
                m_relateStateDic.Put(_triggerId, _stateId);
            }
        }

        /// <summary>
        /// 移除条件关联
        /// </summary>
        /// <param name="_type"></param>
        /// <param name="_triggerId"></param>
        public void RemoveRelation(TID _triggerId)
        {
            var item =  m_triggerList.Find((trigger) => trigger.TriggerId.CompareTo(_triggerId) == 0);
            if (item != null)
            {
                // 回收
                item.Recycle();
            }

            if (_triggerId != null)
                m_relateStateDic.Remove(_triggerId);
        }
        /// <summary>
        /// 获取实际条件
        /// </summary>
        protected abstract BaseTrigger<TID> _GetTrigger(TID _triggerId);

    }
}
